<?php
class Delivery_Model_DbTable_Ciudades
	extends Delivery_Model_DbTable_Abstract

{
	protected $_name = 'ciudades';
	protected $_primary = 'idCiudad';
	
	public function insert( $ciudad )
	{
		return parent::insert(		
			array ('nombre'	=> $ciudad->nombre, 'provincia'	=> $ciudad->provincia) 
		);	
	}
		
	public function getById( $idCiudad )
	{
		return 
			$this->_db->fetchRow(
				$this->select()->where('idCiudad = ?', $idCiudad)
			);
	}
	
	public function getByNombre( $nombre, $provincia = null )
	{
		$select = $this->select()->where('nombre = ?', $nombre);
		
		if( $provincia ){
			$select->where('provincia = ?', $provincia);
		}
		
		return $this->_db->fetchRow( $select );
	}
	
	/**
	 * @param array $ids
	 * @return array:
	 */
	public function getByIds( $ids )
	{
		return $this->getAdapter()->fetchAll(
			$this->select()->where( 'idCiudad IN (?)', $ids, Zend_Db::INT_TYPE),
			null,
			Zend_Db::FETCH_ASSOC
		);		
	}
	
	public function getAll()
	{
		return 
			$this->_db->fetchAssoc(
				$this->select()					
			);
	}
	
	public function getByParams( $params, $offset = null, $limit = null, $order = null )
	{
		$select = $this->select();
			
		if ( $order ){
			$select->order( $order );
		}
		
		if ( $limit && !is_null($offset) ){
			$select->limit( $limit, $offset );
		}
		
		return $this->_db->fetchAssoc($select);
	}
	
	public function countByParams( $params )
	{
		$select = $this->select();
		
		$results = $this->_db->fetchAssoc($select);
		
		return count($results);
	}
	
	
}